<?php
namespace app\index\controller;

use wechat\JSSDK;

class Api extends \think\Controller
{
    public function callBack()
    {
        $origin_url = input("origin_url");
        $code = input("code");
        $app_id = input("appid");
        $app_secret = input("secret");
        dump($app_id);
        dump($app_secret);
        //获取网页的access_token
        $res = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid={$app_id}&secret={$app_secret}&code={$code}&grant_type=authorization_code");
        $res = json_decode($res, true);
       
        //获取access_token失败的话
        if (empty($res['access_token'])) {
            returnData(0, "获取页面授权失败");
            die;
        }
        //组装基本的用户信息数组
        $userinfo = [
            "openid" => $res['openid']
            , "nickname" => ""
            , "username" => ""
            , "sex" => ""
            , "city" => ""
            , "province" => ""
            , "country" => ""
            , "headimgurl" => ""
            , "unionid" => "",
        ];
        //判断是否静默授权，scope为snsapi_base
        if ($res['scope'] == 'snsapi_userinfo') {
            //如果是要获取用户详细信息，就把$userinfo重写下
            $userinfo = file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token={$res['access_token']}&openid={$res['openid']}&lang=zh_CN");
            $userinfo = json_decode($userinfo, true);
        }

        $info = [
            "openid" => $userinfo['openid']
            , "username" => $userinfo['nickname']
            , "sex" => $userinfo['sex']
            , "city" => $userinfo['city']
            , "province" => $userinfo['province']
            , "country" => $userinfo['country']
            , "headimgurl" => $userinfo['headimgurl']
            , "lasttime" => time()
            , "type" => "wechat"
            , "status"=>"1"
            , "ip"=>$this->request->ip()
            , "unionid"=>empty($userinfo['unionid'])?"":$userinfo['unionid']//如果有绑定微信开放平台，则会返回unionid
        ];
        $this->redirect($origin_url."?openid=".$info["openid"]);

    }
    public function getJSSDK()
    {
        //跨越头设置
        header("Access-Control-Allow-Origin: *");
        $app_id = input("appId");
        $app_secret = input("appSecret");
        //生成JSSDK必须的参数
        $jssdk = new JSSDK($app_id, $app_secret);
        //注意，这里要传入请求的地址，因为签名是针对那个请求地址所指页面
        $url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : url("/", "", true, true);
        $signPackage = $jssdk->GetSignPackage($url);
        if (!empty($signPackage)) {
            $res = [
                "res" => 1,
                "msg" => "获取jssdk初始化参数成功",
                "data" => [
                    'appId' => $signPackage["appId"],
                    'timestamp' => $signPackage["timestamp"],
                    'nonceStr' => $signPackage["nonceStr"],
                    'signature' => $signPackage["signature"],
                ],
            ];
        } else {
            $res = [
                "res" => 0,
                "msg" => "获取jssdk初始化参数失败",
                "data" => [],
            ];
        }
        return json($res);
    }
}
